#include "Block.h"

myTetris::Block::Block(const int& block_size, const std::string& shape, const std::string& skin_path)
{
	this->left_up_coordinate = { 0,0 };
	this->block_size = block_size > myTetris::MAX_BLOCK_SIZE ? myTetris::MAX_BLOCK_SIZE : block_size;
	this->block_shape.resize(this->block_size, std::vector<int>(this->block_size, 0));
	this->skins = new IMAGE*[this->block_size];
	for (int i = 0; i < this->block_size; i++)
	{
		this->skins[i] = new IMAGE[this->block_size];
	}
	int n = shape.size();
	std::string cur_skin_root_path = skin_path;
	if (cur_skin_root_path.size() < 15 || cur_skin_root_path.substr(0, 15) != "./Images/Block/")
	{
		cur_skin_root_path = "./Images/Block/" + cur_skin_root_path;
	}
	/*std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
	std::wstring cur_skin_path = converter.from_bytes(all_skin_path);*/
	if (cur_skin_root_path[cur_skin_root_path.size() - 1] != '/')cur_skin_root_path += '/';
	for (int i = 0; i < n; i++)
	{
		//if (shape[i] == '0')continue;
		if (i > this->block_size * this->block_size - 1)break;
		if (!myTetris::shape_char_2_int.count(shape[i]))continue;
		std::string cur_skin_path = cur_skin_root_path;
		int I = i / this->block_size;
		int J = i % this->block_size;
		this->block_shape[I][J] = myTetris::shape_char_2_int[shape[i]];
		cur_skin_path += std::to_string(myTetris::shape_char_2_int[shape[i]]) + ".png";
		loadimage(&this->skins[I][J], cur_skin_path.c_str(), myTetris::SINGAL_BLOCK_LENGTH, myTetris::SINGAL_BLOCK_LENGTH);
	}
}

myTetris::Block::~Block()
{
	for (int i = 0; i < this->block_size; i++)
	{
		delete[] this->skins[i];
	}
	delete[] this->skins;
}

void myTetris::Block::setLeftUpCoordinate(const int& x, const int& y)
{
	this->left_up_coordinate = { x,y };
}

void myTetris::Block::setLeftUpCoordinate(const std::pair<int, int>& coordinate)
{
	this->left_up_coordinate = coordinate;
}

std::pair<int, int> myTetris::Block::getLeftUpCoordinate()
{
	return this->left_up_coordinate;
}

std::vector<std::vector<int>> myTetris::Block::getBlockShape() const
{
	return this->block_shape;
}

int myTetris::Block::getBlockSize()
{
	return this->block_size;
}

IMAGE myTetris::Block::getSkinAt(const int& i, const int& j)
{
	IMAGE ans = this->skins[i][j];
	return ans;
}

void myTetris::Block::draw(const int& x, const int& y)
{
	for (int i = 0; i < this->block_size; i++)
	{
		for (int j = 0; j < this->block_size; j++)
		{
			if (this->block_shape[i][j] == 0)continue;
			putimage(x + (this->left_up_coordinate.second + j) * myTetris::SINGAL_BLOCK_LENGTH, y + (this->left_up_coordinate.first + i) * myTetris::SINGAL_BLOCK_LENGTH, myTetris::SINGAL_BLOCK_LENGTH, myTetris::SINGAL_BLOCK_LENGTH, &this->skins[i][j], 0, 0);
		}
	}
}

void myTetris::Block::drawView(const int& x, const int& y)
{
	for (int i = 0; i < this->block_size; i++)
	{
		for (int j = 0; j < this->block_size; j++)
		{
			if (this->block_shape[i][j] == 0)continue;
			putimage(x + j * myTetris::SINGAL_BLOCK_LENGTH, y + i * myTetris::SINGAL_BLOCK_LENGTH, myTetris::SINGAL_BLOCK_LENGTH, myTetris::SINGAL_BLOCK_LENGTH, &this->skins[i][j], 0, 0);
		}
	}
}
